package models;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import play.Logger;
import play.cache.Cache;
import play.db.helper.SqlQuery.Concat;


import siena.Generator;
import siena.Id;
import siena.Index;
import siena.Model;
import siena.Query;
import siena.Table;
import util.Contant;

@Table("product")
public class ProductModel extends Model{

	@Id(Generator.AUTO_INCREMENT)
	public Long id;
	
	public String name;
	public String image;
	public String descDetail;
	public String alias;
	//for show in product page
	public String descShort;
	public String shortPathImage;
	
	//active product display in FO
	public boolean active;
	
	@Index(value = "category_index")
	public String category;
	
	public ProductModel(String name, String pathImage,
			String description, String alias, String shortDescription,
			String shortPathImage, boolean active, String category) {
		super();
		this.name = name;
		this.image = pathImage;
		this.descDetail = description;
		this.alias = alias;
		this.descShort = shortDescription;
		this.shortPathImage = shortPathImage;
		this.active = active;
		this.category = category;
	}

	static Query<ProductModel> all(){
		return Model.all(ProductModel.class);
	}
	
	public static List<ProductModel> listProducts(){
		Logger.info("--------get list product------------------");
		if(Cache.get(util.Contant.LIST_PRODUCTS) != null){
			return Cache.get(util.Contant.LIST_PRODUCTS, List.class);
		}else{
			List<ProductModel> list = all().fetch();
			Cache.set(util.Contant.LIST_PRODUCTS, list);
			return list;	
		}
	}
	
	//get list active product
	public static List<ProductModel> listActiveProducts(){
		Logger.info("--------get list active product------------------");
		if(Cache.get(util.Contant.LIST_ACTIVE_PRODUCTS) != null){
			return Cache.get(util.Contant.LIST_ACTIVE_PRODUCTS, List.class);
		}else{
			List<ProductModel> list = getListProductFollowStatus(true);
			Cache.set(util.Contant.LIST_ACTIVE_PRODUCTS, list);
			return list;	
		}
	}
	
	//get list hidden product
		public static List<ProductModel> listHiddenProducts(){
			Logger.info("--------get list active product------------------");
			if(Cache.get(util.Contant.LIST_HIDDEN_PRODUCTS) != null){
				return Cache.get(util.Contant.LIST_HIDDEN_PRODUCTS, List.class);
			}else{
				List<ProductModel> list = getListProductFollowStatus(false);
				Cache.set(util.Contant.LIST_HIDDEN_PRODUCTS, list);
				return list;	
			}
		}

	public static ProductModel findFollowAlias(String alias){
		return all().filter("alias", alias).get();
	}
	
	public static List<ProductModel> findByLimit(int page, int limit){
		return all().order("-date").fetch(limit, (page - 1) * Contant.PAGE_ITEM);
	}
	
	public static List<ProductModel> listProductFollowCategory(String category){
		if(Cache.get(Contant.CATEGORY_PRODUCT  + category) != null){
			return Cache.get(Contant.CATEGORY_PRODUCT  + category, List.class);
		}else{
			List<ProductModel> list = all().filter("category", category).fetch();
			Cache.set(Contant.CATEGORY_PRODUCT  + category, list);
			return list;	
		}
		
	}
	
	private static List<ProductModel> getListProductFollowStatus(boolean active){
		return all().filter("active", active).fetch();
	}
	
	//get Product follow Id
	public static ProductModel getProductFollowId(long id){
		if(id != 0){
			return all().getByKey(id);
		}
		return null;
	}
	
	//delete Product follow Id
	public static void deleteProduct(long id){
		ProductModel ProductModel = getProductFollowId(id);
		if(ProductModel != null){
			ProductModel.delete();
		}
	}
	
	//active product
	public static void activeItemProduct(long id){
		ProductModel productModel = getProductFollowId(id);
		if(productModel != null){
			productModel.active = true;
			productModel.update();
		}
	}
	
	//active product
	public static void hiddenItemProduct(long id){
		ProductModel productModel = getProductFollowId(id);
		if(productModel != null){
			productModel.active = false;
			productModel.update();
		}
	}
	
	//get limit list product in List
	public static List<ProductModel> getListProductLimit(List<ProductModel> listProductModelTotals, int page, int limit){
		List<ProductModel> rs = new ArrayList<ProductModel>();
		for(int i = (page - 1)* limit; i<page*limit; i++){
			if(i < listProductModelTotals.size()){
				rs.add(listProductModelTotals.get(i));	
			}
		}
		Logger.info(" limit product " + rs.size());
		return rs;
	}
	
	public static List<ProductModel> listProductRandom(List<ProductModel> listProductModels, int size){
		List<ProductModel> listProductRandom = new ArrayList<ProductModel>();
		for(int i = 0; i < listProductModels.size() ; i++){
			if(i < size){
				Logger.info("size ramdon >> " + listProductRandom.size());
				boolean flag = false;
				Random rnd = new Random();
				ProductModel product = chooseProductRandom(rnd, listProductModels);
				for(ProductModel productItem : listProductRandom){
					if(productItem.equals(product)){
						flag = true;
					}
				}
				if(!flag){
					Logger.info("add product random");
					listProductRandom.add(product);
				}
			}
			
		}
		return listProductRandom;
	}
	
	public static ProductModel chooseProductRandom(Random r, List<ProductModel> listProducts) {
	        int randomInt = r.nextInt(listProducts.size()); // randomList is just a instance variable
	        return listProducts.get(randomInt);
    }

}
